package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {

    @DubboReference
    private ContentService contentService;

    //查询所有广告信息
    @RequestMapping("getContentList")
    @ResponseBody
    public AjaxResult<List<TbContent>> getContentList(){
        try {
            List<TbContent> list = contentService.getContentList();
            return AjaxResult.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //增加广告
    @RequestMapping("addContent")
    @ResponseBody
    public AjaxResult  addContent(TbContent content){
        if (content == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            contentService.addContent(content);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //回显广告信息
    @RequestMapping("editContent")
    @ResponseBody
    public AjaxResult<TbContent> editContent(Long id){
        if (id == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            TbContent content = contentService.editContent(id);
            return AjaxResult.OK().setData(content);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改广告
    @RequestMapping("updateContent")
    @ResponseBody
    public AjaxResult  updateContent(TbContent content){
        if (content == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            contentService.updateContent(content);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除广告
    @RequestMapping("deleteContent")
    @ResponseBody
    public AjaxResult deleteContent(Long [] ids){
        if (ids.length == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            contentService.deleteContent(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

}
